package m.jclecai.com.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.List;

import m.jclecai.com.R;
import m.jclecai.com.bean.MatchResultBean;
import m.jclecai.com.view.CustomExpandListview;

/**
 * Created by hp on 2017/3/16.
 */

public class ZQ_LQ_LotteryDetailsaAdapter extends BaseExpandableListAdapter implements CustomExpandListview.HeaderAdapter{

    private Context context;
    private List<String> dateList;
    private List<List<MatchResultBean.Records>> recordsList;
    private String type;
    CustomExpandListview listview;

    public ZQ_LQ_LotteryDetailsaAdapter(Context context, List<String> dateList,
                                        List<List<MatchResultBean.Records>> recordsList,
                                        String type,
                                        CustomExpandListview listview) {
        this.context = context;
        this.dateList = dateList;
        this.recordsList = recordsList;
        this.type = type;
        this.listview = listview;
    }

    //父控件长度
    @Override
    public int getGroupCount() {
        return recordsList.size();

    }

    //子控件长度
    @Override
    public int getChildrenCount(int groupPosition) {
        return groupPosition < 0 ? 0 : recordsList.get(groupPosition).size() <= 0
                ? 0 : recordsList.get(groupPosition).size();
    }

    //获取父控件（对象）
    @Override
    public Object getGroup(int groupPosition) {
        return recordsList.get(groupPosition);
    }

    //获取子控件（对象）
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return recordsList.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewGroupHolder holder;
        if (convertView == null) {
            holder = new ViewGroupHolder();
            convertView = View.inflate(context, R.layout.zq_lq_lottery_details_group, null);
            holder.tv_date = (TextView) convertView.findViewById(R.id.tv_date);
            convertView.setTag(holder);
        } else {
            holder = (ViewGroupHolder) convertView.getTag();
        }
        holder.tv_date.setText(dateList.get(groupPosition));
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewChildHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.zq_lq_lottery_details_child, null);
            holder = new ViewChildHolder(convertView);
        } else {
            holder = (ViewChildHolder) convertView.getTag();
        }

        holder.init(recordsList.get(groupPosition).get(childPosition));

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public int getHeaderState(int groupPosition, int childPosition) {
        final int childCount = getChildrenCount(groupPosition);
        if (childPosition == childCount - 1) {
            return PINNED_HEADER_PUSHED_UP;
        } else if (childPosition == -1
                && !listview.isGroupExpanded(groupPosition)) {
            return PINNED_HEADER_GONE;
        } else {
            return PINNED_HEADER_VISIBLE;
        }
    }

    @Override
    public void configureHeader(View header, int groupPosition, int childPosition, int alpha) {
        if (groupPosition > -1) {
            TextView tv_date = (TextView) header.findViewById(R.id.tv_date);
            tv_date.setText(dateList.get(groupPosition));
        }
    }

    /**
     * 头部的ViewHolder对象；
     */
    private class ViewGroupHolder {
        TextView tv_date;
    }

    /**
     * 具体的条目
     */
    private class ViewChildHolder {
        TextView tv_date;
        TextView tv_gameName;
        TextView tv_guestName;
        TextView tv_score;
        TextView tv_homeName;

        TextView tv_jjcProjectOne;
        TextView tv_jjcProjectTwo;
        TextView tv_jjcProjectThree;
        TextView tv_jjcProjectFour;

        public ViewChildHolder(View view){

            tv_gameName = (TextView) view.findViewById(R.id.tv_gameName);
            tv_homeName = (TextView) view.findViewById(R.id.tv_homeName);
            tv_score = (TextView) view.findViewById(R.id.tv_score);
            tv_guestName = (TextView) view.findViewById(R.id.tv_guestName);

            tv_jjcProjectOne = (TextView) view.findViewById(R.id.tv_jjcProjectOne);
            tv_jjcProjectTwo = (TextView) view.findViewById(R.id.tv_jjcProjectTwo);
            tv_jjcProjectThree = (TextView) view.findViewById(R.id.tv_jjcProjectThree);
            tv_jjcProjectFour = (TextView) view.findViewById(R.id.tv_jjcProjectFour);
            view.setTag(this);
        }

        public void init(MatchResultBean.Records records){

            showCommomHeader(records);

            if (type.equals("jczq")) {

                showJCZQItem(records);
            } else if (type.equals("jclq")) {

                showJCLQItem(records);
            } else if (type.equals("bjdc")) {

                showBJDCItem(records);
            }
            if("1".equals(records.getState())){
                //比赛取消
                tv_score.setText("取消");
                tv_jjcProjectOne.setText("注:本场比赛官方按照取消处理,赔率(SP)按照1进行计算"); //让球胜平负
                tv_jjcProjectOne.setTextColor(Color.parseColor("#f6851f"));
                tv_jjcProjectTwo.setVisibility(View.GONE); // 总进球
                tv_jjcProjectThree.setVisibility(View.GONE); //半全场
                tv_jjcProjectFour.setVisibility(View.GONE);// 上下单双
            }
        }


        public void showItem(CharSequence item1,CharSequence item2,CharSequence item3,CharSequence item4){
            tv_jjcProjectOne.setText(item1); //让球胜平负
            tv_jjcProjectTwo.setText(item2); // 总进球
            tv_jjcProjectThree.setText(item3); //半全场
            tv_jjcProjectFour.setText(item4);// 上下单双
        }

        /**
         * 竞猜足球的item显示
         * @param records
         */
        public void showJCZQItem(MatchResultBean.Records records){

/*        holder.tv_jjcProjectOne.setText(records.getResult()); //胜平负
        holder.tv_jjcProjectTwo.setText(records.getC_result()); //让分胜平负
        holder.tv_jjcProjectThree.setText(records.getA_result()); //总进球
        holder.tv_jjcProjectFour.setText(records.getH_result());//半全场*/

            CharSequence item1=formatText(records.getResult(),records.getOdds());
            CharSequence item2=getFormatBallText(records);
            CharSequence item3= formatText(records.getA_result(),records.getA_odds());
            CharSequence item4=formatText(records.getH_result(),records.getH_odds());
            showItem(item1,item2,item3,item4);
        }

        public CharSequence getFormatBallText(MatchResultBean.Records records){

            //String str = records.getC_result()+"("+records.getBall()+")",records.getC_odds();

            double v = Double.parseDouble(records.getBall());
            if(v>=0){
                return Html.fromHtml("<html><body>"+records.getC_result()+"<font color='red'>("+records.getBall()+")</font><br/>"+records.getC_odds()+"</body></html>");
            }else{
                return Html.fromHtml("<html><body>"+records.getC_result()+"<font color='"+context.getResources().getColor(R.color.dark_green)+"'>("+records.getBall()+")</font><br/>"+records.getC_odds()+"</body></html>");
            }
        }

        /**
         * 再两个text中添加一个\n换行；
         * @return
         */
        public String formatText(String item1,String item2){
            if(TextUtils.isEmpty(item1)){
                item1="";
            }
            if(TextUtils.isEmpty(item2)){
                item2="";
            }
            return item1+"\n"+item2;
        }


        /**
         * 竞猜蓝球的itemi显示；
         * @param records
         */
        public void showJCLQItem(MatchResultBean.Records records){

 /*       holder.tv_jjcProjectOne.setText(records.getResult()); //胜平负
        holder.tv_jjcProjectTwo.setText(records.getC_result()); //让分胜平负
        if ("大分".equals(records.getX_result()))
            holder.tv_jjcProjectThree.setText(">" + records.getScores()); //大小分
        else
            holder.tv_jjcProjectThree.setText("<" + records.getScores()); //大小分
        holder.tv_jjcProjectFour.setText(records.getD_result());//胜负差*/

            String titleValue="";
            if ("大分".equals(records.getX_result())){
                titleValue=">" + records.getScores();
            }else{
                titleValue="<" + records.getScores();
            }

            CharSequence item1=formatText(records.getResult(),records.getOdds());
            CharSequence item2=formatText(records.getC_result(),records.getC_odds());
            CharSequence item3=formatText(titleValue,records.getX_odds());
            CharSequence item4=formatText(records.getD_result(),records.getD_odds());

            showItem(item1,item2,item3,item4);
        }

        /**
         * 北京单场的ViewHolder显示；
         * @param records
         */
        public void showBJDCItem(MatchResultBean.Records records){
      /*  holder.tv_jjcProjectOne.setText(records.getC_result()); //让球胜平负
        holder.tv_jjcProjectTwo.setText(records.getA_result()); // 总进球
        holder.tv_jjcProjectThree.setText(records.getH_result()); //半全场
        holder.tv_jjcProjectFour.setText(records.getD_result());// 上下单双*/

            String item1=formatText(records.getC_result(),records.getC_odds());
            String item2=formatText(records.getA_result(),records.getA_odds());
            String item3=formatText(records.getH_result(),records.getH_odds());
            String item4=formatText(records.getD_result(),records.getD_odds());
            showItem(item1,item2,item3,item4);
        }

        /**
         * 这里是初始化公共的头部；
         * @param records
         */
        public void showCommomHeader(MatchResultBean.Records records){

            //客队在前，主队在后；
            tv_gameName.setText(records.getGameName() + "\n" + records.getWeek());
            tv_homeName.setText(records.getGuestName());


            tv_score.setText(getScore(records));
            tv_guestName.setText(records.getHomeName());
            tv_jjcProjectTwo.setVisibility(View.VISIBLE); // 总进球
            tv_jjcProjectThree.setVisibility(View.VISIBLE); //半全场
            tv_jjcProjectFour.setVisibility(View.VISIBLE);// 上下单双

            tv_jjcProjectOne.setTextColor(Color.parseColor("#ffaaaaaa"));
        }

        public CharSequence getScore(MatchResultBean.Records records){
            String result="";
            String odd="";

            if (TextUtils.isEmpty(records.getS_result())) {
                if(TextUtils.isEmpty(records.getScore())){
                    result="0:0";
                }else {
                    result=records.getScore();
                }
            }else{
                result=records.getS_result();
            }

            if(!TextUtils.isEmpty(records.getS_odds())){
                odd=records.getS_odds();
            }

            if(TextUtils.isEmpty(odd)){
                return result;
            }else {
                return Html.fromHtml("<html><body>"+result+"<br/><font color='"+Color.parseColor("#ffaaaaaa")+"'>"+odd+"</font></body></html>");
            }
        }
    }
}
